﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Linq.Expressions;
using MongoDB.Driver.Linq;
using Project.Core.Model;

namespace Project.Core.DAL
{
    public abstract class MongodbHelper<T> where T : BaseModel, new()
    {
        public IMongoDatabase AsyncMongoDataBase
        {
            get
            {
                return new MongoDBFactory().Database;
            }
        }

        public IMongoCollection<T> AsyncCollection = null;

        public abstract void SetCollection();

        public MongodbHelper()
        {
            SetCollection();
        }
        #region 方法
        ///// <summary>
        ///// 异步插入
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public abstract Task InsertAsync(T entity);
        ///// <summary>
        ///// 异步插入多条数据
        ///// </summary>
        ///// <param name="entities"></param>
        ///// <returns></returns>
        //public async Task InsertManyAsync(IEnumerable<T> entities)
        //{
        //    await AsyncCollection.InsertManyAsync(entities);
        //}
        ///// <summary>
        ///// 异步重写数据
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public async Task ReplaceOneAsync(T entity)
        //{
        //    await AsyncCollection.ReplaceOneAsync(a => a.ID == entity.ID, entity);
        //}
        ///// <summary>
        ///// 更新数据 FilterDefinition<T>
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <param name="update"></param>
        ///// <returns></returns>
        //public async Task UdateAsync(FilterDefinition<T> filter, UpdateDefinition<T> update)
        //{
        //    await AsyncCollection.UpdateOneAsync(filter, update);
        //}
        ///// <summary>
        ///// 更新数据 Expression<Func<T, bool>>
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <param name="update"></param>
        ///// <returns></returns>
        //public async Task UdateAsync(Expression<Func<T, bool>> filter, UpdateDefinition<T> update)
        //{
        //    await AsyncCollection.UpdateOneAsync(filter, update);
        //}
        ///// <summary>
        ///// 多选删除
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <returns></returns>
        //public async Task DeleteManyAsync(FilterDefinition<T> filter)
        //{
        //    await AsyncCollection.DeleteManyAsync(filter);
        //}
        ///// <summary>
        ///// 异步多选删除
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <returns></returns>
        //public async Task DeleteManyAsync(Expression<Func<T, bool>> filter)
        //{
        //    await AsyncCollection.DeleteManyAsync(filter);
        //}
        ///// <summary>
        ///// 异步删除一条数据 FilterDefinition<T>
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public async Task DeleteOneAsync(FilterDefinition<T> filter)
        //{
        //    await AsyncCollection.DeleteOneAsync(filter);
        //}
        ///// <summary>
        ///// 异步删除一条数据 Expression<Func<T, bool>>
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <returns></returns>
        //public async Task DeleteOneAsync(Expression<Func<T, bool>> filter)
        //{
        //    await AsyncCollection.DeleteOneAsync(filter);
        //}
        ///// <summary>
        ///// 异步查找数据 ExpressionFilterDefinition
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <returns></returns>
        //public async Task FindDataAsync(FilterDefinition<T> filter)
        //{
        //    await AsyncCollection.Find(filter).ToListAsync();
        //}
        ///// <summary>
        ///// 异步查找数据 Expression<Func<T, bool>>
        ///// </summary>
        ///// <param name="filterLinq"></param>
        ///// <returns></returns>
        //public async Task FindDataAsync(Expression<Func<T, bool>> filterLinq)
        //{
        //    await AsyncCollection.Find(filterLinq).ToListAsync();
        //}
        ///// <summary>
        ///// 异步查找数据 Expression<Func<T,bool>>
        ///// </summary>
        ///// <param name="filterLinq"></param>
        ///// <returns></returns>
        //public async Task FindDataLinq(Expression<Func<T, bool>> filterLinq)
        //{
        //    await AsyncCollection.FindAsync<T>(filterLinq);
        //}
        ///// <summary>
        ///// 异步查找并且删除
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <returns></returns>
        //public async Task FindOneAndDeleteAsync(FilterDefinition<T> filter)
        //{
        //    await AsyncCollection.FindOneAndDeleteAsync<T>(filter);
        //}
        ///// <summary>
        ///// 异步查找并且替换
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <param name="replacement"></param>
        ///// <returns></returns>
        //public async Task FindOneAndReplaceAsync(FilterDefinition<T> filter, T replacement)
        //{
        //    await AsyncCollection.FindOneAndReplaceAsync<T>(filter, replacement);
        //}
        ///// <summary>
        ///// 异步查找并且更新
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <param name="update"></param>
        ///// <returns></returns>
        //public async Task FindOneAndUpdateAsync(FilterDefinition<T> filter, UpdateDefinition<T> update)
        //{
        //    await AsyncCollection.FindOneAndUpdateAsync<T>(filter, update);
        //}
        ///// <summary>
        ///// 异步查找并且更新 Expression<Func<T, bool>>
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <param name="update"></param>
        ///// <returns></returns>
        //public async Task FindOneAndUpdateAsync(Expression<Func<T, bool>> filter, UpdateDefinition<T> update)
        //{
        //    await AsyncCollection.FindOneAndUpdateAsync<T>(filter, update);
        //}
        ///// <summary>
        ///// 异步获取总数 FilterDefinition<T>
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <returns></returns>
        //public async Task CountAsync(FilterDefinition<T> filter)
        //{
        //    await AsyncCollection.CountDocumentsAsync(filter);
        //}
        ///// <summary>
        ///// 异步获取总数 Exprssion<Func<T,bool>>
        ///// </summary>
        ///// <param name="filter"></param>
        ///// <returns></returns>
        //public async Task CountAsync(Expression<Func<T, bool>> filter)
        //{
        //    await AsyncCollection.CountDocumentsAsync(filter);
        //}
        #endregion
    }
}
